package cn.edu.nju.software.grapheditor.cmd;

import java.awt.Graphics;
import java.awt.Point;

import cn.edu.nju.software.grapheditor.Drawing;
import cn.edu.nju.software.grapheditor.shape.Segment;

/**@author smy*/
//Completed on 11:20 P.M, May 21th
public class LineCmd extends Command{
	private int beginx=0;
	private int beginy=0;
	private int endx;
	private int endy;
	private Graphics g;
	private Segment tobepainted;
	private Segment previous;
	private boolean newone;
	public LineCmd(Graphics g){
		this.g=g;
	}

	public void executePress(Point p, Drawing dwg) {
		this.beginx=p.x;
		this.beginy=p.y;
		newone=true;
	}

	public void executeDrag(Point p, Drawing dwg) {
		if(this.tobepainted!=null)previous=tobepainted;
		if(newone)previous=null;
		newone=false;
		this.endx=p.x;
		this.endy=p.y;
		tobepainted=new Segment(dwg.getColor(),this.beginx,this.endx,this.beginy,this.endy);
		dwg.addshape(tobepainted);
		dwg.delshape(previous);
		dwg.draw(g);
	}
}
